package com.gfxy.modules.items.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gfxy.modules.system.service.ISysDictService;
import com.gfxy.modules.items.entity.Cjrh;
import com.gfxy.modules.items.mapper.CjrhMapper;
import com.gfxy.modules.items.service.ICjrhService;
import com.gfxy.modules.items.vo.CjrhMemberPage;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;

import org.jeecg.common.system.vo.DictModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @Description: gfxy_t_cjrh
 * @Author: jeecg-boot
 * @Date:   2023-03-24
 * @Version: V1.0
 */
@Service
public class CjrhServiceImpl extends AbstractProjectServiceImpl<CjrhMapper, Cjrh> implements ICjrhService {

	private static final Logger logger = LoggerFactory.getLogger(CjrhServiceImpl.class);

	//todo
	protected int memberCellNum = 5;

	@Autowired
	private ISysDictService sysDictService;

	private Map<String,String> typeNameToIntMap = new HashMap<>();
	private Map<String,String> typeIntToNameMap = new HashMap<>();

	private Map<String,String> ddbStatusNameToIntMap = new HashMap<>();
	private Map<String,String> ddbStatusIntToNameMap = new HashMap<>();

	private Map<String,String> cyxyStatusNameToIntMap = new HashMap<>();
	private Map<String,String> cyxyStatusIntToNameMap = new HashMap<>();

	private Map<String,String> otherLevelNameToIntMap = new HashMap<>();
	private Map<String,String> otherLevelIntToNameMap = new HashMap<>();


	@Override
	public IPage<CjrhMemberPage> selectListPage(String date, Integer pageNo, Integer pageSize) {
		IPage<CjrhMemberPage> page = new Page<>(pageNo, pageSize);
		return page.setRecords(this.baseMapper.selectCjrhMemberPageListByYear(date));
	}

	@Override
	public List<CjrhMemberPage> getCjrhMemberPageListByYear(String date) {
		return this.baseMapper.selectCjrhMemberPageListByYear(date);
	}

	@Override
	public String getCyxyName(Integer cjrhStatus) {
		this.loadDict();

		return this.cyxyStatusIntToNameMap.get(String.valueOf(cjrhStatus));
	}

	@Override
	public String getDdbName(Integer cjrhStatus) {
		this.loadDict();
		return this.ddbStatusIntToNameMap.get(String.valueOf(cjrhStatus));
	}

	@Override
	public String getOtherName(Integer cjrhStatus) {
		this.loadDict();
		return this.otherLevelIntToNameMap.get(String.valueOf(cjrhStatus));
	}

	@Override
	public Double getTotalScore(Cjrh cjrh) {
		double result = 0.0;
		int type = cjrh.getCjrhType();
		int status = cjrh.getCjrhStatus();
		if(type == 1){
			if(status == 1){
				result = 50;
			} else if(status == 2){
				result = 30;
			}
		} else if(type == 2){
			if(status == 1){
				result = 25;
			} else if(status == 2){
				result = 20;
			}
		} else if(type == 3){
			if(status == 1){
				result = 150;
			} else if(status == 2){
				result = 75;
			} else if(status == 3){
				result = 20;
			}
		}
		return result;
	}

	@Override
	protected void pass(String id, String isPass) {

	}

	@Override
	protected Cjrh getOneProject(Row row) {

		this.loadDict();


		String year = ExcelUtil.formatCell(row.getCell(0));

		String name = ExcelUtil.formatCell(row.getCell(1));
		String typeStr = ExcelUtil.formatCell(row.getCell(2));
		String statusStr = ExcelUtil.formatCell(row.getCell(3));
		String levelStr = ExcelUtil.formatCell(row.getCell(4));


		Cjrh cjrh = new Cjrh();
		cjrh.setDate(year);
		cjrh.setProjectName(name);

		Integer type = Integer.parseInt(this.typeNameToIntMap.get(typeStr));
		cjrh.setCjrhType(type);
		if(type.equals(Integer.valueOf(1))){
			cjrh.setCjrhStatus(Integer.parseInt(this.cyxyStatusNameToIntMap.get(statusStr)));
		} else if(type.equals(Integer.valueOf(2))){
			cjrh.setCjrhStatus(Integer.parseInt(this.ddbStatusNameToIntMap.get(statusStr)));
		} else if(type.equals(Integer.valueOf(3))){
			cjrh.setCjrhStatus(Integer.parseInt(this.otherLevelNameToIntMap.get(levelStr)));
		}


		cjrh.setIsPass(0);

		//这个地方不要忘了-算总分
		cjrh.setTotalScore(this.getTotalScore(cjrh));
		return cjrh;
	}

	private void loadDict() {



		this.initDict("CJRH_TYPE", this.typeIntToNameMap, this.typeNameToIntMap);


		this.initDict("CJRH_CYXY_STATUS", cyxyStatusIntToNameMap, cyxyStatusNameToIntMap);


		this.initDict("CJRH_DDB_STATUS", ddbStatusIntToNameMap, ddbStatusNameToIntMap);


		this.initDict("CJRH_OTHER_LEVEL", otherLevelIntToNameMap, otherLevelNameToIntMap);
	}

	private void initDict(String dictName, Map<String,String> intToNameMap, Map<String, String> nameToIntMap){

		if(intToNameMap.isEmpty()|| nameToIntMap.isEmpty()){
			logger.debug("开始查询字典名为[{}] 的字典值", dictName);
			List<DictModel> dictModelList =  sysDictService.queryDictItemsByCode(dictName);

			for (DictModel dictModel : dictModelList) {
				intToNameMap.put(dictModel.getValue(), dictModel.getText());
				nameToIntMap.put(dictModel.getText(), dictModel.getValue());
			}

		}




	}

	@Override
	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}
}
